terraform {
  required_providers {
    aws = {
        source = "aws"
        version = "~> 5.3"
    }
  }
}


module "vpc" {
  source  = "terraform-aws-modules/vpc/aws"
  #version = "5.5.1"
  name = "my-vpc"
  #cidr = "10.12.0./16"
  azs = ["ap-northeast-1a"]
  private_subnets = [ "10.0.1.0/24" ]
  public_subnets = [ "10.0.101.0/24" ]

  enable_nat_gateway = true
  enable_vpn_gateway = true

  tags = {
    terraform = "true"
    Environment = "dev"
  }
}

resource "aws_key_pair" "skey" {
  key_name = "skey"
  public_key = file("~/.ssh/id_rsa.pub")
}

# Get aws free ami
data "aws_ami" "ubuntu" {
  most_recent = true
  filter {
    name = "name"
    values = [ "ubuntu/images/hvm-ssd/ubuntu-*-22.04-amd64-server-*2023*" ]
#    values = [ "ubuntu*22.04*amd64*server*" ]
  }

  owners = [ "099720109477" ]
}

resource "aws_instance" "app" {
 # name = "app"
  ami = data.aws_ami.ubuntu.id
  instance_type = "t2.micro"
  key_name = "skey"
 # subnet_id = module.vpc.aws_subnet.public[0].id
  #subnet_id = aws_vpc.public_subnet.id
#  public_gateway = module.public_gateway.id
 # security_group = module.security_group.id
}